package com.ejuornal.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javassist.expr.NewArray;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.Artical;
import com.ejuornal.bean.Check;
import com.ejuornal.dao.CheckDao;

@Component("checkDao")
public class CheckDaoImpl extends DaoSupport<Check> implements CheckDao {

	public void add(String sql, Object... objs) {
		super.add(sql, objs);
	}
	
	public void batchDelete(String sql, final int... ids) {
		super.batchDelete(sql, ids);
	}

	public List<Check> findById(int articalId) {
		String sql = "select * from t_check where articalId=?";
		List<Check> list = super.getJdbcTemplate().query(sql,
				new Object[] { articalId }, new RowMapper<Check>() {
					public Check mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Check check = new Check();
						check.setArticalId(rs.getInt("articalId"));
						check.setCheckDate(rs.getDate("checkDate"));
						check.setCheckId(rs.getInt("checkId"));
						check.setStatus(rs.getString("status"));
						check.setSuggestion(rs.getString("suggestion"));
						check.setUserId(rs.getInt("userId"));
						return check;
					}
				});
		return list;
	}

	public Check detailCheckInfo(int checkId) {
		String sql = "select * from t_check where checkId = ?";
		Check check = super.getJdbcTemplate().queryForObject(sql,
				new Object[] { checkId }, new RowMapper<Check>() {
					public Check mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Check checkInfo = new Check();
						checkInfo.setArticalId(rs.getInt("articalId"));
						checkInfo.setCheckDate(rs.getDate("checkDate"));
						checkInfo.setCheckId(rs.getInt("checkId"));
						checkInfo.setStatus(rs.getString("status"));
						checkInfo.setSuggestion(rs.getString("suggestion"));
						checkInfo.setUserId(rs.getInt("userId"));
						return checkInfo;
					}
				});
		return check;
	}


	@Override
	public List<Check> findCommitById(int articalId) {
		// TODO Auto-generated method stub
		String  sql= "SELECT DISTINCT c.*  FROM t_check c , t_artical artical ,t_per_periodical perper,t_periodical per, t_editor_commit ecommit "+
				" WHERE  artical.perPeriodicalId =perper.perPeriodicalId AND  perper.periodicalId = per.periodicalId AND "+
				" per.periodicalId = ecommit.periodicalId AND c.articalId = ?  ";
		return getJdbcTemplate().query(sql, new Object[]{articalId}, new RowMapper<Check>(){

			@Override
			public Check mapRow(ResultSet rs, int arg1) throws SQLException {
				Check checkInfo = new Check();
				checkInfo.setArticalId(rs.getInt("articalId"));
				checkInfo.setCheckDate(rs.getDate("checkDate"));
				checkInfo.setCheckId(rs.getInt("checkId"));
				checkInfo.setStatus(rs.getString("status"));
				checkInfo.setSuggestion(rs.getString("suggestion"));
				checkInfo.setUserId(rs.getInt("userId"));
				return checkInfo;
			}
			
		});
	}


	@Override
	public Check findSuggestion(int articalId) {
		String sql = "SELECT a.title,a.postDate, c.* FROM t_artical a ,t_check c WHERE a.articalId = c.articalId AND c.`status`=2 AND a.articalId = ?";
		Check check = super.getJdbcTemplate().queryForObject(sql,  new Object[]{ articalId} , new RowMapper<Check>(){

			@Override
			public Check mapRow(ResultSet rs, int rowNum) throws SQLException {
				Artical artical = new Artical();
				artical.setTitle(rs.getString("a.title"));
				artical.setPostDate(rs.getDate("a.postDate"));
				
				Check checkInfo = new Check();
				checkInfo.setArticalId(rs.getInt("articalId"));
				checkInfo.setCheckDate(rs.getDate("checkDate"));
				checkInfo.setCheckId(rs.getInt("checkId"));
				checkInfo.setStatus(rs.getString("status"));
				checkInfo.setSuggestion(rs.getString("suggestion"));
				checkInfo.setUserId(rs.getInt("userId"));
				
				checkInfo.setArtical(artical);
				return checkInfo;
			}});
		return check;
	}

	

}
